//
//  DHS2_OrderDetailController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/19.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_OrderDetailController.h"
#import "DHS2_OrderDetail_AddressCell.h"
#import "DHS2_PreOrder_ItemCell.h"
#import "DHS2_PreOrder_WashingCell.h"
#import "DHS2_PreOrder_CouponsCell.h"
#import "DHS2_PreOrder_PriceCell.h"
#import "DHS2_OrderManagerController.h"
#import "DHShoppingCartViewController.h"
#import "DHS2_ProductDetailController.h"

@interface DHS2_OrderDetailController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *statuLabel;
@property (strong, nonatomic) NSDictionary *orderDic;

@end

@implementation DHS2_OrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"订单详情";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_OrderDetail_AddressCell" bundle:nil] forCellReuseIdentifier:kDHS2_OrderDetail_AddressCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_PreOrder_ItemCell" bundle:nil] forCellReuseIdentifier:kDHS2_PreOrder_ItemCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_PreOrder_WashingCell" bundle:nil] forCellReuseIdentifier:kDHS2_PreOrder_WashingCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_PreOrder_CouponsCell" bundle:nil] forCellReuseIdentifier:kDHS2_PreOrder_CouponsCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHS2_PreOrder_PriceCell" bundle:nil] forCellReuseIdentifier:kDHS2_PreOrder_PriceCell];
    
    [self loadDetail];
    
}

- (void)backAction{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[DHS2_OrderManagerController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }else if ([controller isKindOfClass:[DHShoppingCartViewController class]]){
            [self.navigationController popToViewController:controller animated:YES];
        }else if ([controller isKindOfClass:[DHS2_ProductDetailController class]]){
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}


- (void)loadDetail{
    [[DHNetAPIClient shareDHNetAPIClient] request_orders_detailWithParams:@{@"order_id":self.order_id} andBlock:^(id data, NSError *error) {
        self.orderDic = [data objectForKey:@"order"];
        [self.tableView reloadData];
        
        __block CGFloat totalPrice = 0;
        NSArray *costArray = [self.orderDic objectForKey:@"cost"];
        for (NSDictionary *dic in costArray) {
            CGFloat value = [[dic objectForKey:@"value"] floatValue];
            totalPrice += value;
        }
        self.priceLabel.text = [NSString stringWithFormat:@"合计：￥%.2f",totalPrice];
        self.statuLabel.text = [self.orderDic objectForKey:@"status"];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSArray *logistics = [self.orderDic objectForKey:@"logistics"];
        if (logistics.count > 0) {
            return 139 + logistics.count * 30;
        }else{
            return 139 + 30;
        }
    }else if (indexPath.row == 1){
        NSArray *leasedArr = [self.orderDic objectForKey:@"subscribed"];
        NSArray *purchasedArr = [self.orderDic objectForKey:@"purchased"];
        NSInteger row;
        if (leasedArr.count > 0 && purchasedArr.count > 0) {
            row = 2;
        }else{
            row = 1;
        }
        return 46 * row + leasedArr.count * 100 + purchasedArr.count * 100 + 10 ;
    }else if (indexPath.row == 2){
        NSArray *washingArr = [self.orderDic objectForKey:@"washing"];
        if (washingArr.count > 0) {
            return 135 + 40 * washingArr.count;
        }else{
            return 0;
        }
    }else if (indexPath.row == 3){
        if ([[self.orderDic objectForKey:@"coupons"]  count] > 0) {
            return 85;
        }else{
            return 0;
        }
    }else if (indexPath.row == 4){
        NSArray *costArray = [self.orderDic objectForKey:@"cost"];
        return 41 + 30 * costArray.count + 5;
    }
    else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHS2_OrderDetail_AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_OrderDetail_AddressCell forIndexPath:indexPath];
        [cell updateCell:self.orderDic];
        return cell;
    }else if (indexPath.row == 1){
        DHS2_PreOrder_ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_PreOrder_ItemCell forIndexPath:indexPath];
        cell.isDetail = YES;
        [cell updateCell:self.orderDic];
        cell.sixView.hidden = YES;
        return cell;
    }else if (indexPath.row == 2){
        DHS2_PreOrder_WashingCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_PreOrder_WashingCell forIndexPath:indexPath];
        [cell updateCell:self.orderDic];
        cell.isDetail = YES;
        NSArray *washingArr = [self.orderDic objectForKey:@"washing"];
        if (washingArr.count == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        return cell;
    }else if (indexPath.row == 3){
        DHS2_PreOrder_CouponsCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_PreOrder_CouponsCell forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.countLabel.text = @"";
        cell.tLabel.text = @"已使用优惠券";
        NSArray *couponArr = [self.orderDic objectForKey:@"coupons"];
        if (couponArr.count > 0) {
            NSDictionary *dic = couponArr[0];
            cell.priceLabel.text = [NSString stringWithFormat:@"-￥%.1f",[[dic objectForKey:@"discount"] floatValue]];
        }
        if ([[self.orderDic objectForKey:@"coupons"]  count] == 0) {
            cell.contentView.hidden = YES;
        }else{
            cell.contentView.hidden = NO;
        }
        return cell;
    }else if (indexPath.row == 4){
        DHS2_PreOrder_PriceCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHS2_PreOrder_PriceCell forIndexPath:indexPath];
        [cell updateCell:self.orderDic];
        return cell;
    }
    else{
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
